package com.team.frame.utils.mail;


import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

import static java.awt.SystemColor.info;

/**
 * Created by lidan on 2016/10/26.
 */
public class Mail {

    final static String USER = "robot"; // 用户名
    final static String PASSWORD = "password520"; // 密码
    public final static String MAIL_SERVER_HOST = "mail.***.com"; // 邮箱服务器
    public final static String TYPE_HTML = "text/html;charset=UTF-8"; // 文本内容类型
    public final static String MAIL_FROM = "[email protected]"; // 发件人
    public final static String MAIL_TO = "[email protected]"; // 收件人
    public final static String MAIL_CC = "[email protected]"; // 抄送人
    public final static String MAIL_BCC = "[email protected]"; // 密送人

    /**
     * 发送非html格式邮件
     * @param fromMail  发件人
     * @param password  第三方授权码
     * @param toMail  收件人
     * @param mailTitle  标题
     * @param mailContent  内容
     * @throws Exception
     */
    public void sendMail(final String fromMail, final String user, final String password, String toMail, String mailTitle,
                         String mailContent) throws Exception {
        Properties props = new Properties();
        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);
        // 创建邮件对象
        Message msg = new MimeMessage(session);
        // 发件人
        msg.setFrom(new InternetAddress("java_mail_001@163.com"));
        // 多个收件人
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("java_mail_002@163.com,java_mail_003@163.com"));
        // 抄送人
//        msg.setRecipient(Message.RecipientType.CC, new InternetAddress("java_mail_001@163.com"));
        // 暗送人
//        msg.setRecipient(Message.RecipientType.BCC, new InternetAddress("java_mail_004@163.com"));
        // 主题
        msg.setSubject("中文主题");
        MimeMultipart mmt = new MimeMultipart();


        StringBuilder builder = new StringBuilder();
        builder.append(mailContent);
        msg.setText(builder.toString());




        msg.setFrom(new InternetAddress(fromMail));
        Transport transport = session.getTransport();
        transport.connect("smtp.qq.com", fromMail, password);
        transport.sendMessage(msg, new Address[]{new InternetAddress(toMail)});
        transport.close();

    }

    /**
     * 自定义html格式邮件
     * @param fromMail
     * @param user
     * @param password
     * @param toMail
     * @throws Exception
     */
    public void sendHtmlMail(final String fromMail, final String user, final String password, String toMail) throws Exception {
        Properties props = new Properties();
        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);
        Message msg = new MimeMessage(session);
        msg.setSubject("激活账号");


       /* //导入自定义html邮件内容
        String info= ReadHTML.reMailString();
        // MimeMultipart类是一个容器类，包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        //创建一个包含HTML内容的MimeBodyPart
        BodyPart body = new MimeBodyPart();
        //设置html内容
        body.setContent(info,"text/html;charset=utf-8");
        mainPart.addBodyPart(body);
        msg.setContent(mainPart);*/
        Multipart multipart = new MimeMultipart();
        BodyPart bodyPart = new MimeBodyPart();
        bodyPart.setContent(info,"测试正文内容");
        //bodyPart.setContent(info,"text/html;charset=utf-8");
        multipart.addBodyPart(bodyPart);

        msg.setContent(multipart);



        msg.setFrom(new InternetAddress(fromMail));
        Transport transport = session.getTransport();
        transport.connect("smtp.qq.com", fromMail, password);
        transport.sendMessage(msg, new Address[]{new InternetAddress(toMail)});
        transport.close();

    }
    /**
     * 随机数
     * @param num
     * @return
     */
/*    public static String getRandom(int num){
        String str = "0,1,2,3,4,5,6,7,8,9";
        String str2[] = str.split(",");//将字符串以,分割
        Random rand = new Random();//创建Random类的对象rand
        int index = 0;
        String randStr = "";//创建内容为空字符串对象randStr
        randStr = "";//清空字符串对象randStr中的值

        for (int i=0; i<num; ++i) {
            index = rand.nextInt(str2.length-1);//在0到str2.length-1生成一个伪随机数赋值给index
            randStr += str2[index];//将对应索引的数组与randStr的变量值相连接
        }

        return randStr;
    }*/
}
